<?php

	require_once '/../Controleur/User_controleur.php';
	require_once '/../Controleur/Personnage_controleur.php';
	require_once '/../Controleur/Episode_controleur.php';
	require_once '/../Controleur/Vote_controleur.php';
        require_once '/../Controleur/Forum_controleur.php';
        require_once '/../Controleur/User_controleur.php';

	session_start();

	if(isset($_POST["username"]) && isset($_POST["password"])) {

		$username = $_POST["username"];
		$password = $_POST["password"];

		$user_controleur = new User_controleur;
		$reponse = $user_controleur->authentification($username, $password);

		if($reponse == null || $reponse == false) {
		   	echo "N"; 
		}
		else{
			if($reponse[0]->getActif() != 1) 
			{
				echo "B";
			} else {
				$_SESSION['username'] = $username;
				$_SESSION['password'] = $password;
				$_SESSION['idUser'] = $reponse[0]->getIdUser();
			    $_SESSION['mail'] = $reponse[0]->getMail();
			    $_SESSION['statut'] = $reponse[0]->getStatut();
			    $_SESSION['timeline'] = $reponse[0]->getTimeline();
                            
                            if($_SESSION['timeline'] != null){
                                $episode=Episode::getById($_SESSION['timeline']);
                                $_SESSION['saison'] = $episode->getIdSaison();
                                $_SESSION['episode'] = $episode->getNumeroEpisode();
                            }
                            else{
                                $_SESSION['saison'] = "1";
                                $_SESSION['episode'] = "1";
                            }
				
			    echo "Y"; 		   	    
		    }
		}

	} else if (isset($_POST["inscr_username"]) && isset($_POST["inscr_password"]) && isset($_POST["inscr_mail"])) {

		$inscr_username = $_POST["inscr_username"];
		$inscr_password = $_POST["inscr_password"];
		$inscr_mail = $_POST["inscr_mail"];

		$user_controleur = new User_controleur;
		$reponseUsername = $user_controleur->verifExisteLogin($inscr_username);
		$reponseMail = $user_controleur->verifExisteMail($inscr_mail);

		if($reponseUsername != null && $reponseUsername != false) {
		   	
		   	if($reponseMail != null && $reponseMail != false) {
		   		echo "B";
		   	} else {
		   		echo "N";
		   	}
		}
		else if($reponseMail != null && $reponseMail != false) {
		    echo "M"; 	
		    	
		}
		else {
			$user_controleur->inscription($inscr_username, $inscr_password, $inscr_mail);
			$reponse = $user_controleur->authentification($username, $password);
			$_SESSION['username'] = $inscr_username;
			$_SESSION['password'] = $inscr_password;
			$_SESSION['mail'] = $inscr_mail;
			$_SESSION['statut'] = 3;
			$_SESSION['idUser'] = $reponse[0]->getIdUser();
			echo "Y";			
		}
	} else if(isset($_POST["newUsername"])) {

			$idUser = $_SESSION['idUser'];
			$newUsername = $_POST["newUsername"];

			$user_controleur = new User_controleur;
			$reponseUsername = $user_controleur->verifExisteLogin($newUsername);

			if($reponseUsername == null || $reponseUsername == false) {
				
				$_SESSION['username'] = $newUsername;

				$user_controleur->changeUsername($idUser, $newUsername);

				echo "Y";
				
			} else {
				echo "N";
			}

	} else if(isset($_POST["newMail"])) {

			$username = $_SESSION['username'];
			$newMail = $_POST["newMail"];

			$user_controleur = new User_controleur;
			$reponseMail = $user_controleur->verifExisteMail($newMail);

			if($reponseMail == null || $reponseMail == false) {
				
				$_SESSION['mail'] = $newMail;

				$user_controleur->changeMail($username, $newMail);

				echo "Y";
				
			} else {
				echo "N";
			}

	} else if(isset($_POST["oldMdp"]) && isset($_POST["newMdp"])) {

			$username = $_SESSION['username'];
			$oldMdp = $_POST["oldMdp"];
			$newMdp = $_POST["newMdp"];

			$user_controleur = new User_controleur;

			if($oldMdp != $_SESSION['password']) {
				echo "N";
			} else {
				
				$user_controleur->changePassword($username, $newMdp);
				$_SESSION['password'] = $newMdp;
				echo "Y";
			}
			
	} else if(isset($_POST["nomPerso"]) && isset($_POST["prenomPerso"])) {

			$nomPerso = $_POST["nomPerso"];
			$prenomPerso = $_POST["prenomPerso"];
			$sexePerso = $_POST["sexePerso"];
			$episodeApp = $_POST["episodeApp"];
			$descrPerso = $_POST["descrPerso"];
			$acteurPerso = $_POST["acteurPerso"];
			$pagePersoActeur = $_POST["pagePersoActeur"];

			$personnage_controleur = new Personnage_controleur;
			$reponse = $personnage_controleur->verifExiste($nomPerso, $prenomPerso);

			if($reponse != null && $reponse != false) {
				echo "N";
			} else {
				
				$personnage_controleur->ajouterPersonnage($nomPerso, $prenomPerso, $sexePerso, $episodeApp, $descrPerso, $acteurPerso, $pagePersoActeur);
				echo "Y";
			}
			
	} else if(isset($_POST["saisonEpisode"]) && isset($_POST["numeroEpisode"])) {

			$saisonEpisode = $_POST["saisonEpisode"];
			$numeroEpisode = $_POST["numeroEpisode"];
			$titreEpisode = $_POST["titreEpisode"];
			$titreEpisodeOriginal = $_POST["titreEpisodeOriginal"];
			$miniResume = $_POST["miniResume"];
			$resume = $_POST["resume"];
			$dateSortie = $_POST["dateSortie"];

			$episode_controleur = new Episode_controleur;
			$reponse = $episode_controleur->verifExiste($saisonEpisode, $numeroEpisode);

			if($reponse != null && $reponse != false) {
				echo "N";
			} else {
				
				$episode_controleur->ajouterEpisode($saisonEpisode, $numeroEpisode, $titreEpisode, $titreEpisodeOriginal, $miniResume, $resume, $dateSortie);
				echo "Y";
			}
			
	} else if(isset($_POST["statut"]) && isset($_POST["idUser"])) {

			$statut = $_POST["statut"];
			$idUser = $_POST["idUser"];
			
			$user_controleur = new User_controleur;		

			if($statut == 3) {
				$user_controleur->changeStatut($idUser,2);
				echo "Y";
			}
			else {
				$user_controleur->changeStatut($idUser,3);
				echo "Y";
			}
			
	} else if(isset($_POST["actif"]) && isset($_POST["idUser"])) {

			$actif = $_POST["actif"];
			$idUser = $_POST["idUser"];
			
			$user_controleur = new User_controleur;
			$reponse = $user_controleur->getInfo($idUser);

			$statut = $reponse->getStatut();

			if($statut == 1) {
				echo"N";
			} else {
				if($actif == 1) {
					$user_controleur->changeActif($idUser,0);
					echo "Y";
				}
				else {
					$user_controleur->changeActif($idUser,1);
					echo "Y";
				}
			}
				
	} else if(isset($_POST["nbPour"]) && isset($_POST["idVote"]) && isset($_POST["idUser"])) {

			$nbPour = $_POST["nbPour"];
			$idVote = $_POST["idVote"];
			$idUser = $_POST["idUser"];
			
			$vote_controleur = new Vote_controleur;
			$vote_controleur->pour($idVote,$nbPour);
			$vote_controleur->confirmeVote($idVote, $idUser, "pour");

			echo "Y";		
				
	} else if(isset($_POST["nbContre"]) && isset($_POST["idVote"]) && isset($_POST["idUser"])) {

			$nbContre = $_POST["nbContre"];
			$idVote = $_POST["idVote"];
			$idUser = $_POST["idUser"];
			
			$vote_controleur = new Vote_controleur;
			$vote_controleur->contre($idVote,$nbContre);
			$vote_controleur->confirmeVote($idVote, $idUser, "contre");


			echo "Y";			
				
	} else if(isset($_POST["idFiche"])) {

			$idFiche = $_POST["idFiche"];
			
			$personnage_controleur = new Personnage_controleur;
			$personnage_controleur->valide($idFiche);

			echo "Y";
                        
    } else if(isset($_POST["nomTopic"])&&isset($_POST["idSection"])) {
			$nomTopic = $_POST["nomTopic"];
			$idSection = $_POST["idSection"];
			$forum_controleur = new Forum_controleur;
			$forum_controleur->creerTopic($idSection,$nomTopic,$_SESSION['idUser'] );
		
				
	}else if(isset($_POST["idTopic"])&&isset($_POST["newMessage"])) {
			$idTopic = $_POST["idTopic"];
			$newMessage = $_POST["newMessage"];
			$forum_controleur = new Forum_controleur;
			$forum_controleur->creerMessage($idTopic,$newMessage,$_SESSION['idUser'] );
		
        }else if(isset($_POST["idMessageSup"])) {          
			$idMessageSup = $_POST["idMessageSup"];
                        var_dump($idMessageSup);
			$forum_controleur = new Forum_controleur;
			$forum_controleur->supprimerMessage($idMessageSup);
		
        }
        else {
		echo "X";
	}
	
?>